Skip to content

Check calls on a constructor call whose constructor has refined parameters - #407

Open
CatarinaGamboa wants to merge 1 commit into
mainfrom
fix/constructor-receiver-refined-args
Open

CatarinaGamboa wants to merge 1 commit into
mainfrom
fix/constructor-receiver-refined-args

Conversation

@CatarinaGamboa

Copy link
Copy Markdown
Collaborator

Follow-up to #361 (typestate calls made directly on a constructor call).

Problem

A call made directly on new T(args) fails when the constructor T(args) has refined parameters:

Socket socket = new ServerSocket(8080).accept();
Argument Mismatch Error: Argument '#<init>_398' and its respective parameter of ghost
'java.net.ServerSocket.state1' types are incompatible: expected ServerSocket, got void

With refined parameters, the constructor call goes through applyReturnRefinement, which sets the call's TARGET metadata to the constructor's return-value instance (#<init>_N, typed void). prepareInvocationTarget checked TARGET before the constructor-call case, so it used that instance as the receiver. Constructors without refined parameters (new ServerSocket().accept()) were not affected.

Fix

  • prepareInvocationTarget handles a constructor-call target before falling back to TARGET metadata.
  • The fresh receiver is stored under a new Keys.RECEIVER key on the constructor call and reused when the same invocation is prepared again (the target is prepared both for the parameter checks and for the state change), so it is created once.

Tests

  • CorrectConstructorReceiverRefinedArgument: new Listener(8080).accept() where Listener(int) has a refined parameter and lands in bound. It failed before this change.
  • ErrorConstructorReceiverRefinedArgument: the same constructor followed by a call that needs unbound is reported.
  • mvn test: 414 tests, 0 failures.

Found on a real fixed bug (a static page server whose fix was new ServerSocket(port).accept()). We also checked 53 exercises built from real OSS bugs: the results are identical to main's.

🤖 Generated with Claude Code

…eters

A call made directly on new T(args), when T(args) has refined parameters,
used the constructor's return-value instance (typed void) as its receiver,
so the state ghost was applied to a void argument and verification failed
with an Argument Mismatch Error. Check the constructor-call receiver before
the TARGET metadata and reuse it across the invocation's checks.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant